package com.wwy.hkapi.HKApi;


import com.wwy.hkapi.HKApi.init.HkInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 海康设备登录
 * @Author: wwy
 * @Date: 2020/8/4 10:24
 */
public class HkLogin {
    private static final Logger LOGGER = LoggerFactory.getLogger(HkLogin.class);
    static HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();// 设备登录信息

    /**
     * 海康设备登录全局用户id集合
     */
    public static ConcurrentHashMap<String,Integer> lUserIDMap = new ConcurrentHashMap<>();
    /**
     * 海康设备登录全局用户账号信息集合
     */
    public static ConcurrentHashMap<String,UserInfo> userInfoMap = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String,HCNetSDK.NET_DVR_DEVICEINFO_V30> deviceInfoMap = new ConcurrentHashMap<>();

    /**
     * 登录
     * @param userInfo
     * @return
     */
    public static int login(UserInfo userInfo){
        HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();// 设备信息
        String deviceIP = userInfo.getIp();
        String username = userInfo.getUsername();
        String password = userInfo.getPassword();
        short port = (short)userInfo.getPort();
        m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
        System.arraycopy(deviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, deviceIP.length());
        m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        System.arraycopy(username.getBytes(), 0, m_strLoginInfo.sUserName, 0, username.length());
        m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
        System.arraycopy(password.getBytes(), 0, m_strLoginInfo.sPassword, 0, password.length());
        m_strLoginInfo.bUseAsynLogin = false; // 是否异步登录：0- 否，1- 是
        m_strLoginInfo.wPort = port;//端口号
        m_strLoginInfo.write();
        int lUserID = HkInit.hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
        int i = HkInit.hCNetSDK.NET_DVR_GetLastError();
        LOGGER.info("登录错误码：{},备注0没有错误",i);
        if(lUserID == -1){
            LOGGER.info("登录失败，登录参数{}",userInfo.toString());
            return lUserID;
        }
        lUserIDMap.put(userInfo.getIp(),lUserID);
        userInfoMap.put(userInfo.getIp(),userInfo);
        deviceInfoMap.put(userInfo.getIp(),m_strDeviceInfo.struDeviceV30);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 struDeviceV30 = m_strDeviceInfo.struDeviceV30;
        LOGGER.info("[硬盘个数:{},设备类型:{},模拟通道个数:{},起始通道号:{},最大数字通道个数低位:{},起始数字通道号:{}]",struDeviceV30.byDiskNum,struDeviceV30.byDVRType,struDeviceV30.byChanNum,struDeviceV30.byStartChan,struDeviceV30.byIPChanNum,struDeviceV30.byStartDChan);
        return lUserID;
    }

    /**
     * 获取userId
     * @param userInfo
     * @return
     */
    public static int getUserId(UserInfo userInfo){
        int lUserID = -1;
        String ip = userInfo.getIp();
        Integer integer = HkLogin.lUserIDMap.get(ip);//用户id
        if(integer == null){
            LOGGER.info("设备未登录进行重新登录");
            //未登录，进行重新登录
            lUserID = HkLogin.login(userInfo);
        }else{
            lUserID = integer;
        }
        UserInfo userInfo1 = HkLogin.userInfoMap.get(ip);
        if(userInfo1 == null){
            LOGGER.info("登录失败{}");
            return -1;
        }
        //判断账号密码是否相同
        String username = userInfo1.getUsername();
        String password = userInfo1.getPassword();
        //账号和密码完全相似不用重新登录
        if(!username.equals(userInfo.getUsername()) && !password.equals(userInfo.getPassword())){
            LOGGER.info("设备账号密码已改变进行重新登录");
            //进行退出操作
            HkLogout.hkLogout(lUserID);
            //进行重新登录
            lUserID = HkLogin.login(userInfo);
        }
        if(lUserID == -1){
            LOGGER.info("登录失败{}");
        }

        return lUserID;
    }
}
